/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.postbar.handler.user;

import cn.ac.iie.di.ban.ycl.idgen.IDGeneratorInterface;
import cn.ac.iie.di.ban.ycl.idgen.IDSHAGeneratorImpl;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import cn.ac.iie.di.postbar.entity.UserEntity;
import cn.ac.iie.di.postbar.handler.user.UserHandler.SessionPool;
import static cn.ac.iie.di.postbar.server.PostQGLtStartUp.provinceMap;
import static cn.ac.iie.di.postbar.server.PostQGLtStartUp.requestServerSize;
import static cn.ac.iie.di.postbar.server.PostQGLtStartUp.userplaceMap;
import static cn.ac.iie.di.postbar.server.PostQGLtStartUp.isTianYa;
import cn.ac.iie.di.postbar.util.Base64;
import cn.ac.iie.di.postbar.util.DeDuplication;
import cn.ac.iie.di.postbar.util.MD5Util;
import cn.ac.iie.di.postbar.util.ParserJson;
import cn.ac.iie.di.postbar.util.TransformUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class UserThreadPoolTask implements Runnable {

    static private final Logger logger = Logger.getLogger(UserThreadPoolTask.class.getName());
    private final List<String> jsonList;
    private final int timeout;
    private final SessionPool pool;
    private long starttime = System.currentTimeMillis() / 1000;

    public UserThreadPoolTask(List<String> jsonList, SessionPool pool, int timeout) {
        this.jsonList = jsonList;
        this.pool = pool;
        this.timeout = timeout;
    }

    @Override
    public void run() {
        RESendSession session = null;
        //计数
        long count = 0;
        //请求去重服务字符串
        String requestString = "";
        //存放user对象
        List<UserEntity> usersEntityList = new ArrayList<UserEntity>();

        try {
            session = pool.borrowSession();
            for (String json : jsonList) {
                if (json != null && !json.isEmpty()) {
                    //解析json
                    List<Map<String, String>> dataMapList = new ArrayList<Map<String, String>>();
                    try {
                        dataMapList = ParserJson.getContentByRecord_list(json);
                        logger.debug("parser json data successfully.");
                    } catch (Exception e) {
                        logger.error("@@@ERROR_MESSAGE@@@:" + json);
                        logger.error("parser data error,cause " + e.getMessage(), e);
                        return;
                    }
                    logger.info("the json has " + dataMapList.size() + " data.");
                    for (int i = 0; i < dataMapList.size(); i++) {
                        UserEntity usersEntity = new UserEntity();
                        Map<String, String> dataMap = new HashMap<String, String>();
                        dataMap = dataMapList.get(i);

                        usersEntity.setG_channel(2);
                        usersEntity.setG_spec("U-BBS07-AI");

//                                                String g_ch_key = dataMap.get("USERID");
//                                                if (g_ch_key == null || g_ch_key.isEmpty() || "null".equals(g_ch_key)) {
//                                                        usersEntity.setG_ch_key("");
//                                                } else {
//                                                        usersEntity.setG_ch_key(g_ch_key);
//                                                }
                        String u_ch_id = dataMap.get("USERID");
                        if (u_ch_id == null || u_ch_id.isEmpty() || "null".equals(u_ch_id)) {
                            usersEntity.setU_ch_id("");
                        } else {
                            usersEntity.setU_ch_id(u_ch_id);
                        }

                        String g_ch_key = "";
//                                                if (isTianYa == 1) {
//                                                        g_ch_key = "tianya.cn_" + usersEntity.getU_ch_id();
//                                                        usersEntity.setG_ch_key(g_ch_key);
//                                                        usersEntity.setG_asp("tianya.cn");
//                                                        usersEntity.setG_adp("tianya.cn");
//                                                } else {
//                                                        g_ch_key = "kdnet.net_" + usersEntity.getU_ch_id();
//                                                        usersEntity.setG_ch_key(g_ch_key);
//                                                        usersEntity.setG_asp("kdnet.net");
//                                                        usersEntity.setG_adp("kdnet.net");
//                                                }
                        if (isTianYa == 1) {
                            g_ch_key = "tianya.cn_" + usersEntity.getU_ch_id();
                            usersEntity.setG_ch_key(g_ch_key);
                            usersEntity.setG_asp("tianya.cn");
                            usersEntity.setG_adp("tianya.cn");
                        } else if (isTianYa == 2) {//人民网强国论坛
                            g_ch_key = "people.com.cn_" + usersEntity.getU_ch_id();
                            usersEntity.setG_ch_key(g_ch_key);
                            usersEntity.setG_asp("people.com.cn");
                            usersEntity.setG_adp("people.com.cn");
                        } else if (isTianYa == 3) {//爱卡
                            g_ch_key = "xcar.com.cn_" + usersEntity.getU_ch_id();
                            usersEntity.setG_ch_key(g_ch_key);
                            usersEntity.setG_asp("xcar.com.cn");
                            usersEntity.setG_adp("xcar.com.cn");
                        } else if (isTianYa == 4) {//铁血
                            g_ch_key = "tiexue.net_" + usersEntity.getU_ch_id();
                            usersEntity.setG_ch_key(g_ch_key);
                            usersEntity.setG_asp("tiexue.net");
                            usersEntity.setG_adp("tiexue.net");
                        } else if (isTianYa == 5) {//中华
                            g_ch_key = "china.com_" + usersEntity.getU_ch_id();
                            usersEntity.setG_ch_key(g_ch_key);
                            usersEntity.setG_asp("china.com");
                            usersEntity.setG_adp("china.com");
                        } else if (isTianYa == 0) {
                            g_ch_key = "kdnet.net_" + usersEntity.getU_ch_id();
                            usersEntity.setG_ch_key(g_ch_key);
                            usersEntity.setG_asp("kdnet.net");
                            usersEntity.setG_adp("kdnet.net");
                        }
                        //ID
                        String id = "";
                        try {
                            IDGeneratorInterface idge = new IDSHAGeneratorImpl();
//                            if (isTianYa == 1) {
//                                id = idge.getId("2", "U-BBS07-AI", "tianya.cn", "tianya.cn", g_ch_key);
//                            } else {
//                                id = idge.getId("2", "U-BBS07-AI", "kdnet.net", "kdnet.net", g_ch_key);
//                            }
                            if (isTianYa == 1) {
                                id = idge.getId("2", "U-BBS07-AI", "tianya.cn", "tianya.cn", g_ch_key);
                            } else if (isTianYa == 2) {//人民网强国论坛
                                id = idge.getId("2", "U-BBS07-AI", "people.com.cn", "people.com.cn", g_ch_key);
                            } else if (isTianYa == 3) {//爱卡
                                id = idge.getId("2", "U-BBS07-AI", "xcar.com.cn", "xcar.com.cn", g_ch_key);
                            } else if (isTianYa == 4) {//铁血
                                id = idge.getId("2", "U-BBS07-AI", "tiexue.net", "tiexue.net", g_ch_key);
                            } else if (isTianYa == 5) {//中华
                                id = idge.getId("2", "U-BBS07-AI", "china.com", "china.com", g_ch_key);
                            } else if (isTianYa == 0) {
                                id = idge.getId("2", "U-BBS07-AI", "kdnet.net", "kdnet.net", g_ch_key);
                            }
                            usersEntity.setG_id(id);
                        } catch (Exception e) {
                            logger.error("(g_id)get id error,cause " + e.getMessage(), e);
                            logger.warn("@@g_ch_key:" + g_ch_key);
                            continue;
                        }

//                                                String u_ch_id = dataMap.get("USERID");
//                                                if (u_ch_id == null || u_ch_id.isEmpty() || "null".equals(u_ch_id)) {
//                                                        usersEntity.setU_ch_id("");
//                                                } else {
//                                                        usersEntity.setU_ch_id(u_ch_id);
//                                                }
                        String u_name = dataMap.get("USERNAME");
                        if (u_name == null || u_name.isEmpty() || "null".equals(u_name)) {
                            usersEntity.setU_name("");
                        } else {
                            try {
                                u_name = new String(Base64.decodeFast(u_name), "UTF-8");
                                usersEntity.setU_name(u_name);
                            } catch (Exception e) {
                                logger.error("(u_name)Decode base64 failed, cause " + e.getMessage(), e);
                                logger.warn("@@u_name:" + dataMap.get("USERNAME"));
                                usersEntity.setU_name("");
                            }
                        }
                        String gender = dataMap.get("USERSEX");
                        if (gender.trim().equals("男")) {
                            usersEntity.setU_gender(1);
                        } else if (gender.trim().equals("女")) {
                            usersEntity.setU_gender(0);
                        } else {
                            usersEntity.setU_gender(-1);
                        }

                        String u_birth = dataMap.get("BIRTHDAY");
                        if (u_birth == null || u_birth.isEmpty() || "null".equals(u_birth)) {
                            usersEntity.setU_birth("");
                        } else {
                            usersEntity.setU_birth(u_birth);
                        }

                        String u_blood_type = dataMap.get("BLOODTYPE");
                        if (u_blood_type == null || u_blood_type.isEmpty() || "null".equals(u_blood_type)) {
                            usersEntity.setU_blood_type("");
                        } else {
                            usersEntity.setU_blood_type(u_blood_type);
                        }

                        String u_work = dataMap.get("CAREER");
                        if (u_work == null || u_work.isEmpty() || "null".equals(u_work)) {
                            usersEntity.setU_work("");
                        } else {
                            usersEntity.setU_work(u_work);
                        }

                        String u_education = dataMap.get("EDUCSTION");
                        if (u_education == null || u_education.isEmpty() || "null".equals(u_education)) {
                            usersEntity.setU_education("");
                        } else {
                            usersEntity.setU_education(u_education);
                        }

                        usersEntity.setU_insert_time(System.currentTimeMillis() / 1000);

                        String u_register_time = dataMap.get("USERRGTTIME");
                        if (u_register_time == null || u_register_time.isEmpty() || "null".equals(u_register_time)) {
                            usersEntity.setU_register_time(-1);
                        } else {
                            try {
                                long str = TransformUtil.timeToLong(u_register_time);
                                usersEntity.setU_register_time(str);
                            } catch (Exception e) {
                                logger.error("(u_register_time)Type conversion error,cause " + e.getMessage(), e);
                                logger.warn("@@u_register_time:" + dataMap.get("BARRGSTTIME"));
                                usersEntity.setU_register_time(-1);
                            }
                        }

                        String u_description = dataMap.get("BRIEFINFO");
                        if (u_description == null || u_description.isEmpty() || "null".equals(u_description)) {
                            usersEntity.setU_description("");
                        } else {
                            usersEntity.setU_description(u_description);
                        }

                        String userplace = dataMap.get("USERPLACE");
                        String provinceid = "";
                        if (userplace == null || userplace.isEmpty() || "null".equals(userplace)) {
                            usersEntity.setU_loc_county(-1);
                            usersEntity.setU_loc_province(-1);
                        } else {
                            try {
                                provinceid = TransformUtil.getPorvinceIdByUserplace(userplace, provinceMap, userplaceMap);
                                if (provinceid.isEmpty()) {
                                    usersEntity.setU_loc_county(-1);
                                    usersEntity.setU_loc_province(-1);
                                } else {
                                    long str = Long.parseLong(provinceid);
                                    usersEntity.setU_loc_county(str);
                                    usersEntity.setU_loc_province(str);
                                }
                            } catch (Exception e) {
                                logger.error("(u_loc_province)Decode base64 failed, cause " + e.getMessage(), e);
                                logger.warn("@@u_loc_province:" + dataMap.get("USERPLACE") + " , and the provinceId:" + provinceid);
                                usersEntity.setU_loc_county(-1);
                                usersEntity.setU_loc_province(-1);
                            }
                        }

//                                                String u_icon_url = dataMap.get("USERPIC");
//                                                if (u_icon_url == null || u_icon_url.isEmpty() || "null".equals(u_icon_url)) {
//                                                        usersEntity.setU_icon_url("");
//                                                } else {
//                                                        try {
//                                                                u_icon_url = new String(Base64.decodeFast(u_icon_url), "UTF-8");
//                                                                usersEntity.setU_icon_url(u_icon_url);
//                                                        } catch (Exception e) {
//                                                                logger.error("(u_icon_url)Decode base64 failed, cause " + e.getMessage(), e);
//                                                                logger.warn("@@u_icon_url:" + dataMap.get("USERPIC"));
//                                                                usersEntity.setU_icon_url("");
//                                                        }
//                                                }
                        String u_icon_url = dataMap.get("USERPIC");
                        if (u_icon_url == null || u_icon_url.isEmpty() || "null".equals(u_icon_url)) {
                            usersEntity.setU_icon_url("");
                            usersEntity.setU_icon_path("");
                        } else {
                            try {
                                u_icon_url = new String(Base64.decodeFast(u_icon_url), "UTF-8");
                                usersEntity.setU_icon_url(u_icon_url);
                                String resCmd = MD5Util.getMd5(u_icon_url);
                                if (isTianYa == 1) {
                                    String u_icon_path = "user/tylt/" + Math.abs(u_icon_url.hashCode()) % 10000 + "/" + resCmd + ".jpg";
                                    usersEntity.setU_icon_path(u_icon_path);
                                } else if (isTianYa == 2) {//人民网强国论坛
                                    String u_icon_path = "user/qglt/" + Math.abs(u_icon_url.hashCode()) % 10000 + "/" + resCmd + ".jpg";
                                    usersEntity.setU_icon_path(u_icon_path);
                                } else if (isTianYa == 3) {//爱卡
                                    String u_icon_path = "user/aklt/" + Math.abs(u_icon_url.hashCode()) % 10000 + "/" + resCmd + ".jpg";
                                    usersEntity.setU_icon_path(u_icon_path);
                                } else if (isTianYa == 4) {//铁血
                                    String u_icon_path = "user/txlt/" + Math.abs(u_icon_url.hashCode()) % 10000 + "/" + resCmd + ".jpg";
                                    usersEntity.setU_icon_path(u_icon_path);
                                } else if (isTianYa == 5) {//中华
                                    String u_icon_path = "user/zhlt/" + Math.abs(u_icon_url.hashCode()) % 10000 + "/" + resCmd + ".jpg";
                                    usersEntity.setU_icon_path(u_icon_path);
                                } else if (isTianYa == 0) {
                                    String u_icon_path = "user/kdlt/" + Math.abs(u_icon_url.hashCode()) % 10000 + "/" + resCmd + ".jpg";
                                    usersEntity.setU_icon_path(u_icon_path);
                                }

                            } catch (Exception e) {
                                logger.error("(u_icon_url)Decode base64 failed, cause " + e.getMessage(), e);
                                logger.warn("@@u_icon_url:" + dataMap.get("USERPIC"));
                                usersEntity.setU_icon_url("");
                                usersEntity.setU_icon_path("");
                            }
                        }
                        String u_message_pub_cnt = dataMap.get("USETPSTNUM");
                        if (u_message_pub_cnt == null || u_message_pub_cnt.isEmpty() || "null".equals(u_message_pub_cnt)) {
                            usersEntity.setU_message_pub_cnt(-1);
                        } else {
                            try {
                                long str = Long.parseLong(u_message_pub_cnt);
                                usersEntity.setU_message_pub_cnt(str);
                            } catch (Exception e) {
                                logger.error("(u_message_pub_cnt)Type conversion error,cause " + e.getMessage(), e);
                                logger.warn("@@u_message_pub_cnt:" + dataMap.get("USETPSTNUM"));
                                usersEntity.setU_message_pub_cnt(-1);
                            }
                        }

                        String u_real_name = dataMap.get("REALNAME");
                        if (u_real_name == null || u_real_name.isEmpty() || "null".equals(u_real_name)) {
                            usersEntity.setU_real_name("");
                        } else {
                            usersEntity.setU_real_name(u_real_name);
                        }

                        String u_anthority_level = dataMap.get("USERRANK");
                        if (u_anthority_level == null || u_anthority_level.isEmpty() || "null".equals(u_anthority_level)) {
                            usersEntity.setU_anthority_level(-1);
                        } else {
                            try {
                                long str = Long.parseLong(u_anthority_level);
                                usersEntity.setU_anthority_level(str);
                            } catch (Exception e) {
                                logger.error("(u_anthority_level)Type conversion error,cause " + e.getMessage(), e);
                                logger.warn("@@u_anthority_level:" + dataMap.get("USERRANK"));
                                usersEntity.setU_anthority_level(-1);
                            }
                        }

                        String u_register_email = dataMap.get("USERCONFIRMEMAIL");
                        if (u_register_email == null || u_register_email.isEmpty() || "null".equals(u_register_email)) {
                            usersEntity.setU_register_email("");
                        } else {
                            usersEntity.setU_register_email(u_register_email);
                        }

                        String u_action_email = dataMap.get("USEREMAIL");
                        if (u_action_email == null || u_action_email.isEmpty() || "null".equals(u_action_email)) {
                            usersEntity.setU_action_email("");
                        } else {
                            usersEntity.setU_action_email(u_action_email);
                        }

                        String u_register_phone = dataMap.get("USERPHONE");
                        if (u_register_phone == null || u_register_phone.isEmpty() || "null".equals(u_register_phone)) {
                            usersEntity.setU_register_phone("");
                        } else {
                            usersEntity.setU_register_phone(u_register_phone);
                        }

                        String u_register_qq = dataMap.get("USERQQ");
                        if (u_register_qq == null || u_register_qq.isEmpty() || "null".equals(u_register_qq)) {
                            usersEntity.setU_register_qq("");
                        } else {
                            usersEntity.setU_register_qq(u_register_qq);
                        }

                        String uo_status = dataMap.get("USERSTATE");
                        if (uo_status == null || uo_status.isEmpty() || "null".equals(uo_status)) {
                            usersEntity.setUo_status(-1);
                        } else if (uo_status.equals("0")) {
                            usersEntity.setUo_status(1);
                        } else if (uo_status.equals("1")) {
                            usersEntity.setUo_status(5);
                        } else if (uo_status.equals("2")) {
                            usersEntity.setUo_status(4);
                        } else {
                            usersEntity.setUo_status(-1);
                            logger.warn("@@uo_status:" + uo_status);
                        }

                        String uo_processed_time = dataMap.get("PROCESSINGTIME");
                        if (uo_processed_time == null || uo_processed_time.isEmpty() || "null".equals(uo_processed_time)) {
                            usersEntity.setUo_processed_time(-1);
                        } else {
                            try {
                                long str = TransformUtil.timeToLong(uo_processed_time);
                                usersEntity.setUo_processed_time(str);
                            } catch (Exception e) {
                                logger.error("(uo_processed_time)Type conversion error,cause " + e.getMessage(), e);
                                logger.warn("@@uo_processed_time:" + dataMap.get("PROCESSINGTIME"));
                                usersEntity.setUo_processed_time(-1);
                            }
                        }

                        String u_snapshottime = dataMap.get("SNAPSHOTTIME");
                        if (u_snapshottime == null || u_snapshottime.isEmpty() || "null".equals(u_snapshottime)) {
                            usersEntity.setU_snapshot_time(-1);
                        } else {
                            try {
                                long str = TransformUtil.timeToLong(u_snapshottime);
                                usersEntity.setU_snapshot_time(str);
                            } catch (Exception e) {
                                logger.error("(u_snapshot_time)Type conversion error,cause " + e.getMessage(), e);
                                logger.warn("@@u_snapshot_time:" + dataMap.get("SNAPSHOTTIME"));
                                usersEntity.setU_snapshot_time(-1);
                            }
                        }

                        String u_experience = dataMap.get("USREXPRCE");
                        if (u_experience == null || u_experience.isEmpty() || "null".equals(u_experience)) {
                            usersEntity.setU_experience(-1);
                        } else {
                            try {
                                long str = Long.parseLong(u_experience);
                                usersEntity.setU_experience(str);
                            } catch (Exception e) {
                                logger.error("(u_experience)Type conversion error,cause " + e.getMessage(), e);
                                logger.warn("@@u_experience:" + dataMap.get("USREXPRCE"));
                                usersEntity.setU_experience(-1);
                            }
                        }

                        String u_fans_bars = dataMap.get("USERFCSBAR");
                        String[] fans_bars = new String[]{};
                        if (u_fans_bars == null || u_fans_bars.isEmpty() || u_fans_bars.equals("[]") || "null".equals(u_fans_bars)) {
                            usersEntity.setU_fans_bars(fans_bars);
                        } else {
                            try {
                                JSONArray jsonArray = JSONArray.fromObject(u_fans_bars);
                                fans_bars = new String[jsonArray.size()];
                                for (int k = 0; k < jsonArray.size(); k++) {
                                    fans_bars[k] = new String(Base64.decodeFast(jsonArray.getString(k)), "UTF-8");
//                                    fans_bars[k] = jsonArray.getString(k);
                                }
                                usersEntity.setU_fans_bars(fans_bars);
                            } catch (Exception e) {
                                logger.error("(u_fans_bars)Field segmentation failed, cause " + e.getMessage(), e);
                                logger.warn("@@u_fans_bars:" + u_fans_bars);
                                usersEntity.setU_fans_bars(new String[]{});
                            }
                        }

                        String u_fans_users = dataMap.get("USERFCSUSERS");
                        String[] fans_users = new String[]{};
                        if (u_fans_users == null || u_fans_users.isEmpty() || u_fans_users.equals("[]") || "null".equals(u_fans_users)) {
                            usersEntity.setU_fans_users(fans_users);
                        } else {
                            try {
                                JSONArray jsonArray = JSONArray.fromObject(u_fans_users);
                                fans_users = new String[jsonArray.size()];
                                for (int k = 0; k < jsonArray.size(); k++) {
                                    fans_users[k] = jsonArray.getString(k);
                                }
                                usersEntity.setU_fans_users(fans_users);
                            } catch (Exception e) {
                                logger.error("(u_fans_users)Field segmentation failed, cause " + e.getMessage(), e);
                                logger.warn("@@u_fans_users:" + u_fans_users);
                                usersEntity.setU_fans_users(new String[]{});
                            }
                        }

                        String u_fans = dataMap.get("USEFANS");
                        String[] fans = new String[]{};
                        if (u_fans == null || u_fans.isEmpty() || u_fans.equals("[]") || "null".equals(u_fans)) {
                            usersEntity.setU_fans(fans);
                        } else {
                            try {
                                JSONArray jsonArray = JSONArray.fromObject(u_fans);
                                fans = new String[jsonArray.size()];
                                for (int k = 0; k < jsonArray.size(); k++) {
                                    fans[k] = jsonArray.getString(k);
                                }
                                usersEntity.setU_fans(fans);
                            } catch (Exception e) {
                                logger.error("(u_fans)Field segmentation failed, cause " + e.getMessage(), e);
                                logger.warn("@@u_fans:" + u_fans);
                                usersEntity.setU_fans(new String[]{});
                            }
                        }

//                        usersEntity.setU_label(new String[]{});
                        String u_label = dataMap.get("LABEL");
                        String[] label = new String[]{};
                        if (u_label == null || u_label.isEmpty() || u_label.equals("[]") || "null".equals(u_label)) {
                            usersEntity.setU_label(label);
                        } else {
                            try {
                                JSONArray jsonArray = JSONArray.fromObject(u_label);
                                label = new String[jsonArray.size()];
                                for (int k = 0; k < jsonArray.size(); k++) {
                                    label[k] = jsonArray.getString(k);
                                }
                                usersEntity.setU_label(label);
                            } catch (Exception e) {
                                logger.error("(u_label)Field segmentation failed, cause " + e.getMessage(), e);
                                logger.warn("@@u_label:" + u_label);
                                usersEntity.setU_label(new String[]{});
                            }
                        }
                        String u_barage = dataMap.get("USERBARAGE");
                        if (u_barage == null || u_barage.isEmpty() || "null".equals(u_barage)) {
                            usersEntity.setU_barage(-1);
                        } else {
                            try {
                                long str = Long.parseLong(u_barage);
                                usersEntity.setU_barage(str);
                            } catch (Exception e) {
                                logger.error("(setU_barage)Type conversion error,cause " + e.getMessage(), e);
                                logger.warn("@@setU_barage:" + dataMap.get("USERBARAGE"));
                                usersEntity.setU_barage(-1);
                            }
                        }

                        String u_last_login_time = dataMap.get("USERLASTLOGINTIME");
                        if (u_last_login_time == null || u_last_login_time.isEmpty() || "null".equals(u_last_login_time)) {
                            usersEntity.setU_last_login_time(-1);
                        } else {
                            try {
                                long str = Long.parseLong(u_last_login_time);
                                usersEntity.setU_last_login_time(str);
                            } catch (Exception e) {
                                logger.error("(u_last_login_time)Type conversion error,cause " + e.getMessage(), e);
                                logger.warn("@@u_last_login_time:" + dataMap.get("USERLASTLOGINTIME"));
                                usersEntity.setU_last_login_time(-1);
                            }
                        }

                        String u_login_num = dataMap.get("USERLOGINNUM");
                        if (u_login_num == null || u_login_num.isEmpty() || "null".equals(u_login_num)) {
                            usersEntity.setU_login_num(-1);
                        } else {
                            try {
                                long str = Long.parseLong(u_login_num);
                                usersEntity.setU_login_num(str);
                            } catch (Exception e) {
                                logger.error("(u_login_num)Type conversion error,cause " + e.getMessage(), e);
                                logger.warn("@@u_login_num:" + dataMap.get("USERLOGINNUM"));
                                usersEntity.setU_login_num(-1);
                            }
                        }

                        String u_industry = dataMap.get("INDUSTRY");
                        if (u_industry == null || u_industry.isEmpty() || "null".equals(u_industry)) {
                            usersEntity.setU_industry("");
                        } else {
                            usersEntity.setU_industry(u_industry);
                        }

                        String u_identification_num = dataMap.get("IDENTIFICATIONNUM");
                        if (u_identification_num == null || u_identification_num.isEmpty() || "null".equals(u_identification_num)) {
                            usersEntity.setU_identification_num("");
                        } else {
                            usersEntity.setU_identification_num(u_identification_num);
                        }

                        String u_address = dataMap.get("ADDRESS");
                        if (u_address == null || u_address.isEmpty() || "null".equals(u_address)) {
                            usersEntity.setU_address("");
                        } else {
                            usersEntity.setU_address(u_address);
                        }

                        String u_postcode = dataMap.get("POSTCODE");
                        if (u_postcode == null || u_postcode.isEmpty() || "null".equals(u_postcode)) {
                            usersEntity.setU_postcode("");
                        } else {
                            usersEntity.setU_postcode(u_postcode);
                        }

//                        usersEntity.setU_belief(new String[]{});
                        String u_belief = dataMap.get("BELIEF");
                        String[] belief = new String[]{};
                        if (u_belief == null || u_belief.isEmpty() || u_belief.equals("[]") || "null".equals(u_belief)) {
                            usersEntity.setU_belief(belief);
                        } else {
                            try {
                                JSONArray jsonArray = JSONArray.fromObject(u_belief);
                                belief = new String[jsonArray.size()];
                                for (int k = 0; k < jsonArray.size(); k++) {
                                    belief[k] = jsonArray.getString(k);
                                }
                                usersEntity.setU_belief(belief);
                            } catch (Exception e) {
                                logger.error("(u_belief)Field segmentation failed, cause " + e.getMessage(), e);
                                logger.warn("@@u_belief:" + u_belief);
                                usersEntity.setU_belief(new String[]{});
                            }
                        }
                        String u_marital_status = dataMap.get("MARITALSTATUS");
                        if (u_marital_status == null || u_marital_status.isEmpty() || "null".equals(u_marital_status)) {
                            usersEntity.setU_marital_status("");
                        } else {
                            usersEntity.setU_marital_status(u_marital_status);
                        }

//                        usersEntity.setU_characters(new String[]{});
                        String u_characters = dataMap.get("CHARACTERS");
						String[] characters = new String[]{};
                        if (u_characters == null || u_characters.isEmpty() || u_characters.equals("[]") || "null".equals(u_characters)) {
                            usersEntity.setU_characters(characters);
                        } else {
                            try {
                                JSONArray jsonArray = JSONArray.fromObject(u_characters);
                                characters = new String[jsonArray.size()];
                                for (int k = 0; k < jsonArray.size(); k++) {
                                   characters[k] = jsonArray.getString(k);
                                }
                                usersEntity.setU_characters(characters);
                            } catch (Exception e) {
                                logger.error("(u_characters)Field segmentation failed, cause " + e.getMessage(), e);
                                logger.warn("@@u_characters:" + u_characters);
                                usersEntity.setU_characters(new String[]{});
                            }
                        }
                        count++;
                        requestString += id + "\n";
                        usersEntityList.add(usersEntity);

                        //请求去重服务并发送数据
                        if (count % requestServerSize == 0 && count != 0) {
                            sendMessageToMQ(usersEntityList, session, requestString);
                            requestString = "";
                            usersEntityList.clear();
                        }
                    }//一条json数据
                }
            }//所有json数据
            if (!usersEntityList.isEmpty()) {
                sendMessageToMQ(usersEntityList, session, requestString);
                count = 0;
                requestString = "";
                usersEntityList.clear();
            }
            logger.info("A total " + count + " data of the user.");
        } catch (Exception ex) {
            logger.error("parser data error, cause " + ex.getMessage(), ex);
        } finally {
            if (session != null) {
                pool.returnSession(session);
            }
        }
    }

    public void sendMessageToMQ(List<UserEntity> usersEntityList, RESendSession session, String requestString) {
        //请求去重服务返回的信息数组
        String[] returnIds = new String[]{};
        long delcount = 0;
        List<UserEntity> realList = new ArrayList<UserEntity>();
        try {
            logger.debug("requestString=" + requestString);
            returnIds = DeDuplication.deduplication(requestString);
        } catch (Exception e) {
            logger.error("request to de-duplication failed, cause " + e.getMessage(), e);
            return;
        }
        //删除重复数据
        for (int s = 0; s < returnIds.length; s++) {
            if ("1".equals(returnIds[s])) {
                delcount++;
                logger.debug("remove one repeat data.");
            } else {
                realList.add(usersEntityList.get(s));
            }
        }
        logger.info("remove datas of the user: " + delcount);

        //循环发送数据
        try {
            for (int b = 0; b < realList.size(); b++) {
                UserEntity entity = realList.get(b);
                if (entity != null) {
                    session.setString("g_id", entity.getG_id());
                    session.setLong("g_channel", entity.getG_channel());
                    session.setString("g_spec", entity.getG_spec());
                    session.setString("g_ch_key", entity.getG_ch_key());
                    session.setString("g_asp", entity.getG_asp());
                    session.setString("g_adp", entity.getG_adp());
                    session.setString("u_ch_id", entity.getU_ch_id());
                    session.setString("u_name", entity.getU_name());
                    session.setLong("u_insert_time", entity.getU_insert_time());
                    session.setLong("u_register_time", entity.getU_register_time());
                    session.setLong("u_loc_county", entity.getU_loc_county());
                    session.setLong("u_loc_province", entity.getU_loc_province());
                    session.setString("u_icon_url", entity.getU_icon_url());
                    session.setLong("u_message_pub_cnt", entity.getU_message_pub_cnt());
                    session.setLong("u_anthority_level", entity.getU_anthority_level());
                    session.setString("u_register_email", entity.getU_register_email());
                    session.setString("u_action_email", entity.getU_action_email());
                    session.setString("u_register_phone", entity.getU_register_phone());
                    session.setString("u_register_qq", entity.getU_register_qq());
                    session.setLong("uo_status", entity.getUo_status());
//                    session.setLong("uo_processed_time", entity.getUo_processed_time());
                    session.setLong("uo_time", entity.getUo_processed_time());
                    session.setLong("u_snapshot_time", entity.getU_snapshot_time());
                    session.setLong("u_experience", entity.getU_experience());
                    session.setStrings("u_fans_bars", entity.getU_fans_bars());
                    session.setStrings("u_fans_users", entity.getU_fans_users());
                    session.setStrings("u_fans", entity.getU_fans());
                    session.setLong("u_gender", entity.getU_gender());
                    session.setString("u_birth", entity.getU_birth());
                    session.setString("u_blood_type", entity.getU_blood_type());
                    session.setString("u_work", entity.getU_work());
                    session.setString("u_education", entity.getU_education());
                    session.setString("u_description", entity.getU_description());
                    session.setString("u_real_name", entity.getU_real_name());
                    session.setStrings("u_label", entity.getU_label());
                    session.setLong("u_barage", entity.getU_barage());
                    session.setLong("u_last_login_time", entity.getU_last_login_time());
                    session.setLong("u_login_num", entity.getU_login_num());
                    session.setString("u_industry", entity.getU_industry());
                    session.setString("u_identification_num", entity.getU_identification_num());
                    session.setString("u_address", entity.getU_address());
                    session.setString("u_postcode", entity.getU_postcode());
                    session.setStrings("u_belief", entity.getU_belief());
                    session.setString("u_marital_status", entity.getU_marital_status());
                    session.setStrings("u_characters", entity.getU_characters());
                    session.setString("u_icon_path", entity.getU_icon_path());
                    try {
                        session.add();
                    } catch (Exception e) {
                        sendMSG(session);
                        session.add();
                    }

                    if (System.currentTimeMillis() / 1000 - starttime > timeout) {
                        starttime = System.currentTimeMillis() / 1000;
                        sendMSG(session);
                        logger.info("timeout.");
                    }
                }
            }
            sendMSG(session);
            logger.info("send datas of user: " + realList.size());
        } catch (Exception ex) {
            logger.error("setSession error, cause " + ex.getMessage(), ex);
        }
    }

    public void sendMSG(RESendSession session) {
        while (true) {
            try {
                logger.info("flush...");
                session.flush();
                break;
            } catch (Exception ex) {
                logger.info("what's up buddy ? how's it going ?" + ex, ex);
            }
        }
    }
}
